'''
Author: Sjy
Description: 数组list 带有增删功能 允许编辑 注意只有两列 一列数值 一列操作
'''
# -*- coding: utf-8 -*-

from PyQt5 import  QtWidgets, QtCore, QtGui

from .ui_tableEditedDataList import Ui_tableEditedDataList

# 此组件初始时必须有一行两列
class Ui_tableToolboxEditedDataList(QtWidgets.QWidget):
    sigAverage = QtCore.pyqtSignal()
    def __init__(self, params:dict={}, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
        mainLayout = QtWidgets.QVBoxLayout()
        self.__tableWidget = Ui_tableEditedDataList(params)
        btnBox = QtWidgets.QHBoxLayout()
        # 上移
        self.__btnMoveUp = QtWidgets.QPushButton('向上')
        self.__btnMoveUp.clicked.connect(self._moveUp)
        self.__btnMoveUp.setIcon(QtGui.QIcon('icons/up.png'))
        self.__btnMoveUp.setIconSize(QtCore.QSize(20, 20))
        # 置顶
        self.__btnMoveTop = QtWidgets.QPushButton('置顶')
        self.__btnMoveTop.clicked.connect(self._moveTop)
        self.__btnMoveTop.setIcon(QtGui.QIcon('icons/top.png'))
        self.__btnMoveTop.setIconSize(QtCore.QSize(20, 20))
        # 下移
        self.__btnMoveDown = QtWidgets.QPushButton('向下')
        self.__btnMoveDown.clicked.connect(self._moveDown)
        self.__btnMoveDown.setIcon(QtGui.QIcon('icons/down.png'))
        self.__btnMoveDown.setIconSize(QtCore.QSize(20, 20))
        # 置底
        self.__btnMoveBottom = QtWidgets.QPushButton('置底')
        self.__btnMoveBottom.clicked.connect(self._moveBottom)
        self.__btnMoveBottom.setIcon(QtGui.QIcon('icons/bottom.png'))
        self.__btnMoveBottom.setIconSize(QtCore.QSize(20, 20))
        # 添加行
        self.__btnAdd = QtWidgets.QPushButton('添加')
        self.__btnAdd.clicked.connect(self._add)
        self.__btnAdd.setIcon(QtGui.QIcon('icons/add_circle.png'))
        self.__btnAdd.setIconSize(QtCore.QSize(20, 20))
        # 删除行
        self.__btnDel = QtWidgets.QPushButton('删除')
        self.__btnDel.clicked.connect(self._del)
        self.__btnDel.setIcon(QtGui.QIcon('icons/remove_circle.png'))
        self.__btnDel.setIconSize(QtCore.QSize(20, 20))

        # 
        self.__btnClear = QtWidgets.QPushButton('清空')
        self.__btnClear.clicked.connect(self._clear)
        self.__btnClear.setIcon(QtGui.QIcon('icons/clear.png'))
        self.__btnClear.setIconSize(QtCore.QSize(20, 20))

        # 范围等值
        self.__btnAverage = QtWidgets.QPushButton('平均分布')
        self.__btnAverage.clicked.connect(self._average)
        self.__btnAverage.setIcon(QtGui.QIcon('icons/average.png'))
        self.__btnAverage.setIconSize(QtCore.QSize(20, 20))

        btnBox.addWidget(self.__btnAdd)
        btnBox.addWidget(self.__btnDel)
        btnBox.addWidget(self.__btnClear)
        btnBox.addWidget(self.__btnAverage)
        btnBox.addWidget(self.__btnMoveUp)
        btnBox.addWidget(self.__btnMoveDown)
        btnBox.addWidget(self.__btnMoveTop)
        btnBox.addWidget(self.__btnMoveBottom)
        btnBox.addStretch(1)

        mainLayout.addLayout(btnBox)
        mainLayout.addWidget(self.__tableWidget)

        self.setLayout(mainLayout)
    
    # 上移动
    def _moveUp(self):
        # 当前选中行向上移动
        # 注意如果最上则不允许移动
        curRow = self.__tableWidget.currentRow()
        errorMsg = self.__tableWidget._moveUp()
        if errorMsg:
            QtWidgets.QMessageBox.warning(self, '提示', errorMsg)
        else:
            self.__tableWidget._updateAllFocus(curRow-1)
    # 置顶
    def _moveTop(self):
        errorMsg = self.__tableWidget._moveTop()
        if errorMsg:
            QtWidgets.QMessageBox.warning(self, '提示', errorMsg)
        self.__tableWidget._updateAllFocus(0)
    # 下移
    def _moveDown(self):
        curRow = self.__tableWidget.currentRow()
        errorMsg = self.__tableWidget._moveDown()
        if errorMsg:
            return QtWidgets.QMessageBox.warning(self, '提示', errorMsg)
        else:
            self.__tableWidget._updateAllFocus(curRow+1)
    # 置底
    def _moveBottom(self):
        errorMsg = self.__tableWidget._moveBottom()
        if errorMsg:
            return QtWidgets.QMessageBox.warning(self, '提示', errorMsg)
        self.__tableWidget._updateAllFocus(self.__tableWidget.rowCount()-1)
    # 添加行
    def _add(self):
        rowIndex = self.__tableWidget._addRow()
        self.__tableWidget._updateAllFocus(rowIndex)

    # 删除行
    def _del(self):
        count = self.__tableWidget.rowCount()
        if count <=0 :
            return QtWidgets.QMessageBox.warning(self, '提示', '没有数据行')
        curRow = self.__tableWidget.currentRow()
        if curRow == None or curRow == -1:
            return QtWidgets.QMessageBox.warning(self, '提示', '请选中要删除的行')

        # QtWidgets.QMessageBox.question  中的按钮显示中文名 
        # reply = QtWidgets.QMessageBox.question(self, '提示', '是否删除?', QtWidgets.QMessageBox.Yes|QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
        # if reply != QtWidgets.QMessageBox.Yes:
        #     return
        msgBox = QtWidgets.QMessageBox()
        msgBox.setWindowTitle("提示")
        msgBox.setInformativeText("是否删除?")
        yes_button = msgBox.addButton("是", QtWidgets.QMessageBox.YesRole)
        no_button = msgBox.addButton("否", QtWidgets.QMessageBox.NoRole)
        msgBox.exec_()
        if msgBox.clickedButton() != yes_button:
            return 

        errorMsg = self.__tableWidget._removeRow()
        if errorMsg:
            return QtWidgets.QMessageBox.warning(self, '提示', errorMsg)
        
        if self.__tableWidget.rowCount() != 0:
            self.__tableWidget._updateAllFocus(curRow)

    # 清除全部
    def _clear(self):
        # 确认清空提示 注意如果没有数据则不需要
        if self.__tableWidget.rowCount() == 0:
            return
        reply = QtWidgets.QMessageBox.question(self, '提示', '是否清空所有数据行？', QtWidgets.QMessageBox.Yes|QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
        if reply != QtWidgets.QMessageBox.Yes:
            return
        self.__tableWidget._clear()

    # 平均分布
    def _average(self):
        # 触发 平均分布 信号 因为可能每个处理的界面不同 除了允许提供默认的处理方式 也可以自定义
        self.sigAverage.emit()
        # self.__tableWidget._average()


    # 获取数据
    def _get(self):
        return self.__tableWidget._get()
    # 设置数据
    def _set(self, data:list=[]):
        self.__tableWidget._set(data)



